import { ref, onMounted } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import { getCategoryAPI } from '@/apis/category'

export const useCategory = () => {
  // 获取分类数据
  const route = useRoute()
  const categoryData = ref({})
  const getCategory = async (id = route.params.id) => {
    const res = await getCategoryAPI(id)
    categoryData.value = res.result
  }
  onMounted(() => getCategory())
  //目标 路由参数变化的时候，可以把分类数据接口重新发送
  onBeforeRouteUpdate((to) => getCategory(to.params.id))
  return {
    categoryData
  }
}